package com.tanhua.app.service;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.tanhua.api.db.UserInfoApi;
import com.tanhua.api.mongo.CommentApi;
import com.tanhua.db.UserInfo;
import com.tanhua.mongo.Comment;
import com.tanhua.uti.RelativeDateFormat;
import com.tanhua.vo.CommentVo;
import com.tanhua.vo.PageBeanVo;
import com.tanhua.vo.UserHolder;
import org.apache.dubbo.config.annotation.Reference;
import org.bson.types.ObjectId;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Service
public class CommentService {
    @Reference
    private CommentApi commentApi;

    @Reference
    private UserInfoApi userInfoApi;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    public ResponseEntity findCommentVoByPage(Integer pageNum, Integer pageSize, String publishId) {
        PageBeanVo pageBeanVo = commentApi.findCommentByPage(pageNum, pageSize, publishId);
        List<CommentVo> voList = new ArrayList<>();

        List<Comment> commentList = (List<Comment>) pageBeanVo.getItems();
        if (CollectionUtil.isNotEmpty(commentList)) {
            for (Comment comment : commentList) {
                //查询用户信息
                UserInfo userInfo = userInfoApi.findByUserId(comment.getUserId());

                CommentVo commentVo = new CommentVo();

                BeanUtils.copyProperties(userInfo,commentVo);
                commentVo.setContent(comment.getContent());
                commentVo.setLikeCount(comment.getLikeCount());
                commentVo.setCreateDate(RelativeDateFormat.format(new Date(comment.getCreated())));
                commentVo.setId(comment.getId().toString());
               //高亮显示
                Boolean flag = stringRedisTemplate.hasKey(StrUtil.format("publish_comment_like_{}_()", comment.getId(),UserHolder.getUser().getId()));
                if (flag==true){
                    commentVo.setHasLiked(1);
                }else{
                    commentVo.setHasLiked(0);
                }
                voList.add(commentVo);
            }

        }
        pageBeanVo.setItems(voList);
        return ResponseEntity.ok(pageBeanVo);
    }

    public ResponseEntity saveComment(String publishId, String content) {
        Comment comment = new Comment();
        comment.setCreated(System.currentTimeMillis());//评论时间
        comment.setPublishId(new ObjectId(publishId));//动态id
        comment.setContent(content);//评论内容
        comment.setCommentType(2);//类型：评论
        comment.setPubType(1);//类型：动态
        comment.setUserId(UserHolder.getUser().getId());//评论人ID
        commentApi.save(comment);
        return ResponseEntity.ok(null);
    }


    public ResponseEntity commentLike(String commentId) {
        Integer likeCount = commentApi.update(commentId);
        //记录到redis里面(设置"1"表示已经点赞，用于高亮显示)
        stringRedisTemplate.opsForValue().set(StrUtil.format("publish_comment_like_{}_()", commentId,UserHolder.getUser().getId()), "1");
        return ResponseEntity.ok(likeCount);

    }

    public ResponseEntity commentDislike(String commentId) {
        Integer likeCount = commentApi.delete(commentId);
        stringRedisTemplate.delete(StrUtil.format("publish_comment_like_{}_()", commentId,UserHolder.getUser().getId()));
        return ResponseEntity.ok(likeCount);
    }
}
